﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 小组日均值
    /// </summary>
    public class ServiceGroupAvgDay : Benefit.Interface.Day.IGroupAvgDay
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupAvgDay(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.GroupAvgDay where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.GroupAvgDay c in query)
            {
                db.GroupAvgDay.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存小组均值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitGroupDayAvg(int tradeHistoryId)
        {
            List<Models.Sys.Groups> groups = db.Groups.ToList();
            Interface.Sys.IDayGroupOperators iop = new Sys.ServiceDayGroupOperators(db);
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayOperatorAccount ioa = new Sys.ServiceDayOperatorAccount(db);
            foreach (Models.Sys.Groups g in groups)
            {
                Models.Day.GroupAvgDay day = new Models.Day.GroupAvgDay();
                day.GroupId = g.Id;
                day.TradeHistoryId = tradeHistoryId;
   
    
                List<Operator> operators = iop.GetGroupOperators(g.Id, ith.GetDateTimeFromTradeHistoryId(tradeHistoryId));

                List<Models.Day.AccountDayChange> change1 = new List<Models.Day.AccountDayChange>();
                List<Models.Day.AccountDayAnalysis> change2 = new List<Models.Day.AccountDayAnalysis>();

                foreach (Models.Sys.Operator oper in operators)
                {
          
                    List<Account> accounts = ioa.GetDayOperatorAccounts(ith.GetPdateFromTradeHistoryId(tradeHistoryId), oper.Id);
                    foreach (Account acc in accounts)
                    {
                        var query1 = from t in db.AccountDayChange where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query1.Count() > 0)
                        {
                            change1.Add(query1.First());
                        }

                        var query2 = from t in db.AccountDayAnalysis where t.AccountId == acc.Id && t.TradeHistoryId == tradeHistoryId select t;
                        if (query2.Count() > 0)
                        {
                            change2.Add(query2.First());
                        }
                    }
                }
                if (change1.Count > 0)
                {
                    day.BillCount = Math.Round(change1.Average(a => a.BillCount), 2);
                    day.DayCount = Math.Round(change1.Average(a => a.DayCount), 2);
                    day.Free = Math.Round(change1.Average(a => a.Free), 2);
                    day.LoseCount = Convert.ToInt32(change1.Average(a => a.LoseCount));
                    day.Profit = Math.Round(change1.Average(a => a.Profit), 2);
                    day.SumLost = Math.Round(change1.Average(a => a.SumLost), 2);
                    day.SumWin = Math.Round(change1.Average(a => a.SumWin), 2);
                    day.UsedMargin = Math.Round(change1.Average(a => a.UsedMargin), 2);
                    day.WinCount = Convert.ToInt32(change1.Average(a => a.WinCount));
                }

                if (change2.Count > 0)
                {
                    day.AvgBillBenefit = Math.Round(change2.Average(a => a.AvgBillBenefit), 2);
                    day.AvgPostionTime = Math.Round(change2.Average(a => a.AvgPostionTime), 2);
                    day.MaxReturn = Math.Round(change2.Average(a => a.MaxReturn), 2);
                    day.OverStopLoss = Math.Round(change2.Average(a => a.OverStopLoss), 2);
                    day.PingJinPing = Convert.ToInt32(change2.Average(a => a.PingJinPing));
                    day.RaiseRate = Math.Round(change2.Average(a => a.RaiseRate), 2);
                    day.RiskCount = Convert.ToInt32(change2.Average(a => a.RiskCount));
                    day.Utilization = Math.Round(change2.Average(a => a.Utilization), 2);
                    day.WinRate = Math.Round(change2.Average(a => a.WinRate), 2);
                    day.Yield = Math.Round(change2.Average(a => a.Yield), 2);
                }
                db.GroupAvgDay.Add(day);
            }
            db.SaveChanges();
        }



        /// <summary>
        /// 获取小组月均值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public Models.Day.GroupAvgDay GetGroupAvgDay(int groupid, int tradeHistoryId)
        {
            Models.Day.GroupAvgDay count = new Models.Day.GroupAvgDay();
            var query = db.GroupAvgDay.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.GroupId == groupid);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }


    }
}